package com.tuanzi.loan.web.bean.compensation;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.compensation.CompensationOrderService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.compensation.CompensationOrderRequest;
import com.tuanzi.loan.vo.response.compensation.CompensationOrderResponse;
import com.tuanzi.loan.vo.response.compensation.CompensationOrderTotalResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.compensation.CompensationOrderLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.COMPENSATION_ORDER_STATUS;

/**
 * @author administrator
 * @createTime 2019-06-06 16:19
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CompensationOrderListBean extends
        JSFListingBean<CompensationOrderRequest,CompensationOrderResponse> {
    @Autowired
    private CompensationOrderService compensationOrderService;

    private CompensationOrderTotalResponse total;

    @PostConstruct
    public void init() throws Exception {
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new CompensationOrderRequest();
        LocalDateTime date = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        Instant instant = date.atZone(ZoneId.systemDefault()).toInstant();
        $.setStartTime(Date.from(instant));
        $.setEndTime(new Date());
        lazyDataModel = new CompensationOrderLazyDataModel($);
        total = compensationOrderService.total($);
    }

    public void onSearch() throws Exception{
        total = compensationOrderService.total($);
    }

    public void onReset() throws Exception{
        initLazyDataModel();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(COMPENSATION_ORDER_STATUS);
    }
}

